/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.commands.Command;
import com.bourre.commands.CommandManagerMS;
import com.bourre.commands.Delegate;
import com.bourre.commands.MacroCommand;
import com.bourre.data.collections.IndexedArray;
import com.bourre.data.iterator.Iterator;
import com.bourre.events.IEvent;

import fever.log.FeverDebug;
import fever.utils.Stringifier;

/**
 * Batch command process each {@code n} milliseconds.
 * 
 * @author Romain Ecarnot
 */
class fever.commands.DelayCommandManager implements MacroCommand
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _nDelay : Number;
	private var _aC : IndexedArray;
	private var _oEvent : IEvent;	private var _oIterator : Iterator;
	private var _bPending : Boolean;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Default delay between commands execution. */
	public static var DEFAULT_COMMAND_DELAY : Number = 500;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * <p>If {@code delay} is not specified use the 
	 * {@link #DEFAULT_COMMAND_DELAY} value.
	 * 
	 * @param delay Delay between commands execution
	 */ 
	public function DelayCommandManager( delay : Number ) 
	{
		_aC = new IndexedArray();
		
		_nDelay = ( delay > 0 ) ? delay : DEFAULT_COMMAND_DELAY;
		
		_bPending = false;
	}	
	
	/**
	 * Adds {@code command } to manager.
	 * 
	 * @param oCommand Command instance to add.
	 */
	public function addCommand( command : Command ) : Void
	{
		_aC.push( command );
	}
	
	/**
	 * Removes {@code command } from manager.
	 * 
	 * @param oCommand Command instance to add.
	 */
	public function removeCommand( command : Command ) : Void
	{
		_aC.remove( command );
	}
	
  	/**
	 * Removes all commands.
	 * 
	 * <p>Each command added will be removed, it means that they won't 
	 * be executed anymore.
	 */
	public function removeAllCommands() : Void
	{
		_aC.init();
	}
	
	/**
	 * Executes stored commands.
	 */
	public function execute( e : IEvent ) : Void
	{
		if( !_bPending )
		{
			_bPending = true;
			
			_oEvent = e;
			_oIterator = _aC.getIterator();
			
			_executeNextCommand();
		}
		else
		{
			FeverDebug.WARN( 'Track process is already running' );	
		}
	}
	
	/**
	 * Returns amount of stored commands.
	 */
  	public function getLength() : Number
  	{
  		return _aC.length;
  	}
  
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _executeNextCommand( o : Command ) : Void
  	{
  		if( _oIterator.hasNext() )
  		{
  			var oC : Command = _oIterator.next();
  			oC.execute( _oEvent );
  			
  			CommandManagerMS.getInstance().delay( new Delegate( this, _executeNextCommand ), _nDelay );	
  		}
  		else _bPending = false;
  	}
}